package ai.assist.admin.controller.system;

import com.anwen.mongo.conditions.query.LambdaQueryChainWrapper;
import com.anwen.mongo.model.PageParam;
import com.anwen.mongo.model.PageResult;
import ai.assist.admin.vo.ClickLogVO;
import ai.assist.common.annotation.LogAnnotation;
import ai.assist.common.annotation.LogIgore;
import ai.assist.common.annotation.PassportSSO;
import ai.assist.common.businesss.mongo.entity.SysLog;
import ai.assist.common.businesss.mongo.service.SysLogService;
import ai.assist.common.dto.admin.QueryLogDTO;
import ai.assist.common.utils.CachedBeanCopier;
import ai.assist.common.utils.DateUtilsEx;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统操作日志
 *
 * @version V1.0
 */
@RequestMapping("/sys")
@Api(tags = "日志管理", value = "系统操作日志管理")
@RestController
@CrossOrigin(originPatterns = "*", methods = {RequestMethod.GET, RequestMethod.POST})
public class SysLogController {

    @Resource
    private SysLogService logService;

    @PostMapping("/logs")
    @ApiOperation(value = "分页查询系统操作日志接口")
    @LogIgore
    @PassportSSO
    @LogAnnotation(title = "系统操作日志管理", action = "分页查询系统操作日志")
    public Object pageInfo(@RequestBody QueryLogDTO vo) {
        PageParam page = new PageParam(vo.getPage(), vo.getSize());
        LambdaQueryChainWrapper queryWrapper = logService.lambdaQuery();
        if (StringUtils.isNotBlank(vo.getOrder()) && StringUtils.isNotBlank(vo.getSort())) {
            if (vo.getSort().equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(vo.getOrder());
            } else {
                queryWrapper.orderByDesc(vo.getOrder());
            }
        } else {
            queryWrapper.orderByDesc("create_time");
        }
        queryWrapper.like(StringUtils.isNoneBlank(vo.getUsername()), "username", vo.getUsername());
        queryWrapper.like(StringUtils.isNoneBlank(vo.getRealName()), "realName", vo.getRealName());
        queryWrapper.like(StringUtils.isNoneBlank(vo.getOperation()), "operation", vo.getOperation());
        queryWrapper.gt(vo.getStartTime() != null, "create_time", vo.getStartTime());
        queryWrapper.lt(vo.getEndTime() != null, "create_time", vo.getEndTime());
        return logService.page(queryWrapper, page);
    }

    @GetMapping("/logs")
    @ApiOperation(value = "分页查询系统操作日志接口")
    @LogIgore
    @PassportSSO(ignore = true)
    public Object logs(@RequestParam("page") Integer page, @RequestParam("pageSize") Integer pageSize, @RequestParam(value = "start", required = false) String start, @RequestParam(value = "end", required = false) String end) {
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = 10;
        }
        PageParam pageInfo = new PageParam(page, pageSize);
        LambdaQueryChainWrapper queryWrapper = logService.lambdaQuery().gte(StringUtils.isNotBlank(start), SysLog::getCreateTime, DateUtilsEx.getDayStart(DateUtilsEx.parseDayByDay(start)))
                .lte(StringUtils.isNotBlank(end), SysLog::getCreateTime, DateUtilsEx.getDayEnd(DateUtilsEx.parseDayByDay(end))).orderByDesc(SysLog::getCreateTime);
        PageResult<SysLog> list = logService.page(queryWrapper, pageInfo);
        PageResult<ClickLogVO> pageResult = new PageResult<>();
        CachedBeanCopier.copyObject(list, pageResult);
        List<ClickLogVO> voList = new ArrayList<>();
        if (list != null && !list.getContentData().isEmpty()) {
            for (SysLog log : list.getContentData()) {
                ClickLogVO vo = new ClickLogVO();
                vo.setId(log.getId());
                vo.setRealName(log.getUsername());
                vo.setOperation(log.getOperation());
                vo.setClickTime(DateUtilsEx.getStringDateTime(log.getCreateTime()));
                voList.add(vo);
            }
        }
        pageResult.setContentData(voList);
        return pageResult;
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除日志接口")
    @LogAnnotation(title = "系统操作日志管理", action = "删除系统操作日志")
    public Object deleted(@RequestBody List<String> logIds) {
        return logService.removeBatchByIds(logIds);
    }

}
